<?php 
    
/**
 * 响应消息
 */
abstract class WechatSendMsg
{
    public $toUserName;   // 开发者微信号
    public $fromUserName; // 发送方帐号（一个OpenID）
    public $createTime;   // 消息创建时间 （整型）
    public $msgType;      // 文本类型
    
    /**
     * 初始化响应消息
     */
    public function __construct($data)
    {
        $this->toUserName   = $data['to'];
        $this->fromUserName = $data['from'];
        $this->msgType      = $data['type'];
        $this->createTime   = time();
    }

    /**
     * 响应微信服务器的消息
     */
    public function responseMsg()
    {
        $xml = $this->makeMsg();
        $this->makeContent($xml);
        $msg = $this->makeMsg();
        
        de("\n-----------sendmsg---------\n".$msg);
        echo $msg;
    }
    
    /**
     * 构建消息实体
     */
    public function makeMsg()
    {
        $xml =<<<EOF
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
%s
</xml>
EOF;
        return sprintf($xml,
            $this->toUserName,
            $this->fromUserName,
            time(),
            $this->msgType,
            $this->makeContent());
    }
    
    /**
     * 构建内容
     */
    abstract public function makeContent();
}


/**
 * 发送文本消息
 */
class WechatSendTextMsg extends WechatSendMsg
{
    public $content;

    /**
     * 初始化文本消息
     */
    public function __construct($data)
    {
        parent::__construct($data);
        $this->content = $data['content'];
    }

    /**
     * 构建文本消息主要内容
     */
    public function makeContent()
    {
        return '<Content><![CDATA['.$this->content.']]></Content>';
    }
}


/**
 * 图片消息
 */
class WechatSendImageMsg extends WechatSendMsg
{
    public $picUrl;
    public $mediaId;

    /**
     * 初始化文本消息
     */
    public function __construct($data)
    {
        parent::__construct($data);
        $this->picUrl  = $data['picUrl'];
        $this->mediaId = $data['mediaId'];
    }

    /**
     * 构建文本消息主要内容
     */
    public function makeContent()
    {
        return '<Image><MediaId><![CDATA['.$this->mediaId.']]></MediaId></Image>';
    }
}


/**
 * 音频消息
 */
class WechatSendVoiceMsg extends WechatSendMsg
{
    public $mediaId;

    /**
     * 初始化文本消息
     */
    public function __construct($data)
    {
        parent::__construct($data);
        $this->mediaId = $data['mediaId'];
    }

    /**
     * 构建文本消息主要内容
     */
    public function makeContent()
    {
        return '<Voice><MediaId><![CDATA['.$this->mediaId.']]></MediaId></Voice>';
    }
}

/**
 * 视频消息
 */
class WechatSendVideoMsg extends WechatSendMsg
{
    public $mediaId;
    public $title;
    public $desc;

    /**
     * 初始化消息
     */
    public function __construct($data)
    {
        parent::__construct($data);
        $this->mediaId = $data['mediaId'];
        $this->title = $data['title'];
        $this->desc = $data['desc'];
    }

    /**
     * 构建消息主要内容
     */
    public function makeContent()
    {
        return '<Video>
<MediaId><![CDATA['.$this->mediaId.']]></MediaId>
<Title><![CDATA['.$this->title.']]></Title>
<Description><![CDATA['.$this->desc.']]></Description>
</Video>';
    }   
}